package com.qf.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.03.11
 * @since 1.0
 */
@Component
public class ConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback ,RabbitTemplate.ReturnCallback{

    @Autowired
    RabbitTemplate rabbitTemplate;

    //bean生命周期  初始化的时候执行init-method  销毁的时候执行destroy-method
    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(ack){
            System.out.println("成功的发送到交换机");
        }else{
            System.out.println("发送到交换机失败");
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String s, String s1, String s2) {

        System.out.println("失败的到达队列");
    }
}
